package net.sf.odinms.exttools.doclet.pages;

import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;
import com.sun.javadoc.PackageDoc;
import com.sun.tools.doclets.internal.toolkit.util.DocletConstants;
import net.sf.odinms.exttools.doclet.AbstractPageWriter;
import net.sf.odinms.exttools.doclet.Doclet;

/**
 * Creates "package-list", a listing of all documented packages.
 */
public final class PackageList extends AbstractPageWriter {
	
	/**
	 * Generate the package listing.
	 */
	public static void generatePackageList() {
		try {
			new PackageList();
		}
		catch (IOException exc) {
			throw Doclet.exception(exc, DocletConstants.PACKAGE_LIST_FILE_NAME);
		}
	}
	
	/**
	 * Generate list of documented packages.
	 * 
	 * @throws IOException If {@link java.io.FileOutputStream} creation fails.
	 */
	private PackageList() throws IOException {
		super(DocletConstants.PACKAGE_LIST_FILE_NAME);
		// Using a TreeSet guarantees insertion in ascending order.
		Set<String> packages = new TreeSet<String>();
		for (PackageDoc pkg : CONF.packages)
			packages.add(pkg.name());
		for (String pkgName : packages)
			println(pkgName);
		
		this.close();
	}
	
}
